package com.zcoj.controller.Admin.user;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Role;
import com.zcoj.pojo.Dto.User;
import com.zcoj.pojo.Dto.UserRole;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.RoleService;
import com.zcoj.service.UserRoleService;
import com.zcoj.service.UserService;
import com.zcoj.util.RedisCaches;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/18 12:44
 */
@RestController
@RequestMapping("/adminUser")
@Slf4j
@PreAuthorize("hasAnyAuthority('super_admin','admin')")
public class adminUserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RedisCaches redisCaches;

    @PostMapping("/userList")
    public ApiResponse userList(@RequestBody PageWrap<User> pageWrap){
        return ApiResponse.success(userService.findPage(pageWrap));
    }

    @GetMapping("/roleList")
    public ApiResponse roleList(){
        LoginUser loginUser = redisCaches.getLoginUser();
        if(Objects.nonNull(loginUser)){
            Role role = new Role();
            role.setRole(loginUser.getPermission().get(0));
            return ApiResponse.success(roleService.findListDesc(role));
        }else {
            return ApiResponse.failed("角色表初始化失败");
        }
    }
    @GetMapping("/delUser/{id}")
    public ApiResponse delUser(@PathVariable Integer id){
        Integer integer = userService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateUser")
    public ApiResponse updateUser(@RequestBody User user){
        Integer updateById = userService.updateById(user);
        if(updateById!=0){
            return ApiResponse.success("更新成功",null);
        }else {
            return ApiResponse.failed("更新失败！！！");
        }
    }

    @PostMapping("/updateUserRole")
    public ApiResponse updateUserRole(@RequestBody Map<String,Integer> userRole){
        LoginUser loginUser = redisCaches.getLoginUser();

        System.out.println(userRole.toString());
        Integer rid = userRole.get("rid");
        Integer uid = userRole.get("uid");
        if(Objects.equals(loginUser.getId(),uid)){
            return ApiResponse.failed("不可修改自己的角色");
        }
        UserRole userRole1 = new UserRole();
        userRole1.setUid(uid);
        UserRole one = userRoleService.findOne(userRole1);
        if(Objects.nonNull(one)){
            one.setRid(rid);
            userRoleService.updateById(one);
            return ApiResponse.success("角色更新成功",null);
        }else {
            userRole1.setRid(rid);
            userRoleService.create(userRole1);
            return ApiResponse.success("角色添加成功");
        }
    }

    @GetMapping("/getUserById/{id}")
    public ApiResponse getUserById(@PathVariable Integer id){
        return ApiResponse.success( userService.findById(id));
    }

    @GetMapping("/getUserRoleById/{id}")
    public ApiResponse getUserRoleById(@PathVariable Integer id){
        UserRole userRole = new UserRole();
        userRole.setUid(id);
        UserRole one = userRoleService.findOne(userRole);
        if(Objects.nonNull(one)){
            return ApiResponse.success(one.getRid());
        }else {
            return ApiResponse.failed("用户无绑定角色");
        }
    }



}
